package com.doradance.app.home;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.cncn.youmengsharelib.ShareListener;
import com.cncn.youmengsharelib.SharePlatform;
import com.cncn.youmengsharelib.ui.SharePlatformsFragment;
import com.doradance.R;
import com.doradance.account.UrlCenter;
import com.doradance.account.utils.MyGridView;
import com.doradance.account.utils.XCRoundRectImageView;
import com.doradance.app.mine.StartVideoActivity;
import com.doradance.common.http.MyHttpRequest;
import com.doradance.entity.StarStudentEntity;
import com.pachong.android.baseuicomponent.activity.BaseActivity;
import com.pachong.android.framework.httprequest.responsecallbacks.DataRequestListener;

import java.util.List;

import butterknife.Bind;

public class StarStudentActivity extends BaseActivity {

    @Bind(R.id.iv_star_head)
    ImageView mIvStarHead;
    @Bind(R.id.iv_head_information)
    XCRoundRectImageView mIvHeadInformation;
    @Bind(R.id.tv_name)
    TextView mTvName;
    @Bind(R.id.tv_persional_sex)
    TextView mTvPersionalSex;
    @Bind(R.id.tv_family_name)
    TextView mTvFamilyName;
    @Bind(R.id.tv_constellation)
    TextView mTvConstellation;
    @Bind(R.id.tv_persional_birthday)
    TextView mTvPersionalBirthday;
    @Bind(R.id.tv_persion_height)
    TextView mTvPersionHeight;
    @Bind(R.id.tv_personal_weight)
    TextView mTvPersonalWeight;
    @Bind(R.id.tv_persional_origin)
    TextView mTvPersionalOrigin;
    @Bind(R.id.tv_persional_evaluate)
    TextView mTvPersionalEvaluate;
    @Bind(R.id.gv_pic)
    MyGridView mGvPic;
    List<String> show_images;
    PictureAdapter pictureAdapter;
    StarStudentEntity o1;
    private int id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mGvPic.setFocusable(false);

        id = getIntent().getIntExtra("id", -1);
        initToolsBar(id);
    }

    private void initToolsBar(int id) {
        getCustomToolbar().addLeftImageButton(R.mipmap.back, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        MyHttpRequest request = new MyHttpRequest(this);
        String format = String.format(UrlCenter.STARSTUDENTS, id);
        request.get(format, null, new DataRequestListener<StarStudentEntity>(StarStudentEntity.class) {
            @Override
            public void onSuccess(StarStudentEntity o) {
                super.onSuccess(o);
                o1 = o;
                getCustomToolbar().addRightImageButton(R.mipmap.icon_share, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        shareStudent();
                    }
                });
                setTitle(o.getUsername());
                Glide.with(getApplicationContext()).load(o.getCover()).dontAnimate()
                        .placeholder(R.mipmap.init_logo).into(mIvStarHead);
                Glide.with(getApplicationContext()).load(o.getAvatar()).asBitmap().dontAnimate()
                        .placeholder(R.mipmap.init_logo).into(mIvHeadInformation);
                mTvName.setText(o.getUsername());
                if (o.getGender() == 1) {
                    mTvPersionalSex.setText("男");
                } else {
                    mTvPersionalSex.setText("女");
                }
                mTvFamilyName.setText(o.getNation());
                mTvConstellation.setText(o.getConstellation());
                mTvPersionalBirthday.setText(o.getBirthday());
                mTvPersionHeight.setText(o.getHeight_text());
                mTvPersonalWeight.setText(o.getWeight_text());
                mTvPersionalOrigin.setText(o.getOrigin());
                mTvPersionalEvaluate.setText(o.getEvaluate());
                show_images = o.getShow_images();

                pictureAdapter = new PictureAdapter();
                mGvPic.setAdapter(pictureAdapter);
                mGvPic.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        final String s = show_images.get(position);
                        final Dialog dialog = new Dialog(StarStudentActivity.this, R.style.Transparent);
                        dialog.setContentView(R.layout.activity_big_pic);
                        ImageView mIvBigPic = (ImageView) dialog.findViewById(R.id.iv_big_pic);
                        Window w = dialog.getWindow();
                        WindowManager.LayoutParams lp = w.getAttributes();
                        lp.x = 0;
                        lp.y = 40;
                        dialog.onWindowAttributesChanged(lp);
                        Glide.with(StarStudentActivity.this).load(s).asBitmap().into(mIvBigPic);
                        mIvBigPic.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                dialog.cancel();
                            }
                        });
                        dialog.show();
                    }
                });

            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);

            }
        });

    }

    private void shareStudent() {
        SharePlatformsFragment fragment = SharePlatformsFragment.newInstance(o1.getUsername(), o1.getEvaluate(), o1.getAvatar(), "http://h5.doradance.cn/#/starInfo?id="+id);
        fragment.setShareListener(new ShareListener() {
            @Override
            public void onShareSuccess(Activity shareActivity, SharePlatform platform) {
                Toast.makeText(StarStudentActivity.this, "分享成功", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onShareFailed(Activity shareActivity, SharePlatform platform) {
                Toast.makeText(StarStudentActivity.this, "分享失败", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onShareCancel(Activity shareActivity, SharePlatform platform) {
                Toast.makeText(StarStudentActivity.this, "取消分享", Toast.LENGTH_SHORT).show();
            }
        });
        fragment.show(getSupportFragmentManager(), "");
    }

    public static void start(Context activity, int id) {
        Intent intent = new Intent(activity, StarStudentActivity.class);
        intent.putExtra("id", id);
        activity.startActivity(intent);
    }

    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_star_student, null);
    }



    public class PictureAdapter extends BaseAdapter{

        @Override
        public int getCount() {

            return show_images.size();
        }

        @Override
        public Object getItem(int position) {
            return show_images.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if(convertView==null){
                convertView = View.inflate(getApplicationContext(), R.layout.gridview_item, null);
            }
            ImageView ivGvItem = (ImageView) convertView.findViewById(R.id.iv_gv_item);
            Glide.with(getApplicationContext()).load(show_images.get(position)).asBitmap().into(ivGvItem);
            return convertView;
        }

    }
}
